#define _GNU_SOURCE
#define __USE_GNU
#include <unistd.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <sched.h>
#include <errno.h>

#include <malloc.h>

int target(void *arg) {
    sg_write(2, "target was run\n", sizeof("target was run\n"));
    //sleep(20);
    return 0;
}

#define USE_CLONE_THREAD 1

struct timespec t = {10, 0};
/*I don't think it would hurt to make this void*,
but void** is what's used in http://www.linuxjournal.com/article/5211*/
void** stack;

int main(int argc, char* argv[]) {
    int res;
    stack = (void**)malloc(16384);
    if(!stack)
        perror("Error allocting stack");
    
    res = clone(target, 
                stack,
                #if USE_CLONE_THREAD
                    CLONE_THREAD|
                #endif
                CLONE_SIGHAND|CLONE_VM|CLONE_FS|CLONE_FILES,
                NULL //fn arg
                );
    if(res < 0)
        perror("Error creating process");
    
    /*sleep for 10sec*/
    syscall(SYS_nanosleep, &t, NULL);
    sg_write(2, "sleep finished\n", sizeof("sleep finished\n"));
    
    return 0;
}